package com.wanxi.servlet;

import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/del")
public class ProductDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String sort = req.getParameter("sort");
//        3.封装对象
        ProductModel productModel = ProductModel.builder()
                .id(Tool.strToInt(id))
                .sort(Tool.strToInt(sort))
                .name(name)
                .build();
//        4.处理逻辑
        ProductService productService = new ProductService();
        productService.del(productModel);
//        5.返回响应
        resp.sendRedirect("/findAll");
    }
}
